package cn.lhl01.util;

import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;

import java.util.HashMap;
import java.util.Map;

public class PasswordUtil {

	/**
	 * 参数加解密 对称的
	 */
	public static final String AES_KEY = "abcdefgabcdefg12";

	/**
	 * 加密方式
	 */
	public final static String BCRYPT = "bcrypt";
	public final static String NOOP = "noop";
	public final static String PBKDF2 = "pbkdf2";
	public final static String SCRYPT = "scrypt";
	public final static String ARGON2 = "argon2";

	private final static String encodingIdDefault = BCRYPT; //默认值 和 spring保持一致
	//参考spring提供的PasswordEncoderFactories类 创建  获取默认的PasswordEncoder  为BCryptPasswordEncoder
	private static Map<String, PasswordEncoder> encoders = new HashMap<>();

	static {
		encoders.put(BCRYPT, new BCryptPasswordEncoder());
		//明文的
		encoders.put(NOOP, NoOpPasswordEncoder.getInstance());
		encoders.put(PBKDF2, new Pbkdf2PasswordEncoder());
		encoders.put(SCRYPT, new SCryptPasswordEncoder());
		encoders.put(ARGON2, new Argon2PasswordEncoder());
	}

	/**
	 * 通过encodingId指定默认值
	 *
	 * @param encodingId
	 * @return
	 */
	public static PasswordEncoder getPasswordEncoder(String encodingId) {
		return new DelegatingPasswordEncoder(encodingId, encoders);
	}

	/**
	 * 默认的  参照springSecurity 提供的PasswordEncoderFactories 的默认值
	 *
	 * @return
	 */
	public static PasswordEncoder getDefaultPasswordEncoder() {
		return new DelegatingPasswordEncoder(encodingIdDefault, encoders);
	}

}
